package com.ideal.hbase.client;

import com.ideal.hbase.client.core.LimitEventHandler;
import com.ideal.hbase.client.core.TableCallback;
import com.ideal.hbase.client.core.mapper.ResultsExtractor;
import com.ideal.hbase.client.core.mapper.RowMapper;
import com.ideal.hbase.client.core.mapper.ValueMapper;
import com.sematext.hbase.wd.AbstractRowKeyDistributor;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Delete;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Increment;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;

import java.util.List;

public interface HbaseOperations {
	/**
	 *	从给定表中获取单个行。内容由给定的  action 映射。
	 *
	 * @param tableName
	 *            target table
	 * @param rowName
	 *            row name
	 * @param mapper
	 *            row mapper
	 * @return object mapping the target row
	 */
	<T> T get(TableName tableName, byte[] rowName, final RowMapper<T> mapper);

	/**
	 *	从给定表中获取单个行。内容由给定的  action 映射。
	 *
	 * @param tableName
	 *            target table
	 * @param rowName
	 *            row name
	 * @param familyName
	 *            column family
	 * @param mapper
	 *            row mapper
	 * @return object mapping the target row
	 */
	<T> T get(TableName tableName, byte[] rowName, byte[] familyName,
			final RowMapper<T> mapper);

	/**
	 * 	从给定表中获取单个行。内容由给定的  action 映射。
	 *
	 * @param tableName
	 *            target table
	 * @param rowName
	 *            row name
	 * @param familyName
	 *            family
	 * @param qualifier
	 *            column qualifier
	 * @param mapper
	 *            row mapper
	 * @return object mapping the target row
	 */
	<T> T get(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier,
			final RowMapper<T> mapper);

	<T> T get(TableName tableName, final Get get, final RowMapper<T> mapper);

	<T> List<T> get(TableName tableName, final List<Get> get,
			final RowMapper<T> mapper);
	
	<T> List<T> findParallel(TableName tableName, final Scan scan, final AbstractRowKeyDistributor rowKeyDistributor, final RowMapper<T> action, int numParallelThreads);
    <T> List<T> findParallel(TableName tableName, final Scan scan, final AbstractRowKeyDistributor rowKeyDistributor, int limit, final RowMapper<T> action, int numParallelThreads);
    <T> List<T> findParallel(TableName tableName, final Scan scan, final AbstractRowKeyDistributor rowKeyDistributor, int limit, final RowMapper<T> action, final LimitEventHandler limitEventHandler, int numParallelThreads);
    <T> T findParallel(TableName tableName, final Scan scan, final AbstractRowKeyDistributor rowKeyDistributor, final ResultsExtractor<T> action, int numParallelThreads);

	

	void put(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier, final byte[] value);

	void put(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier,
			final Long timestamp, final byte[] value);

	<T> void put(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier, final T value,
			final ValueMapper<T> mapper);

	<T> void put(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier,
			final Long timestamp, final T value, final ValueMapper<T> mapper);

	void put(TableName tableName, final Put put);

	void put(TableName tableName, final List<Put> puts);
	 void batchPut(TableName tableName,final byte[] rowName, final List<Put> puts);


	/**
	 * 如果异步操作未设置，则执行PUT方法而不是异步Put方法。
	 */
	boolean asyncPut(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier, final byte[] value);

	/**
	 * 如果异步操作未设置，则执行PUT方法而不是异步Put方法。
	 */
	boolean asyncPut(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier,
			final Long timestamp, final byte[] value);

	/**
	 * 如果异步操作未设置，则执行PUT方法而不是异步Put方法。
	 */
	<T> boolean asyncPut(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier, final T value,
			final ValueMapper<T> mapper);

	/**
	 * 如果异步操作未设置，则执行PUT方法而不是异步Put方法。
	 */
	<T> boolean asyncPut(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier,
			final Long timestamp, final T value, final ValueMapper<T> mapper);

	/**
	 * 如果异步操作未设置，则执行PUT方法而不是异步Put方法。
	 */
	boolean asyncPut(TableName tableName, final Put put);

	/**
	 * 如果异步操作未设置，则执行PUT方法而不是异步Put方法。
	 * 
	 * @param tableName
	 * @param puts
	 */
	List<Put> asyncPut(TableName tableName, final List<Put> puts);

	void delete(TableName tableName, final Delete delete);

	void delete(TableName tableName, final List<Delete> deletes);

	<T> List<T> find(TableName tableName, final List<Scan> scans,
			final ResultsExtractor<T> action);

	<T> List<List<T>> find(TableName tableName, final List<Scan> scans,
			final RowMapper<T> action);

	<T> List<T> findParallel(TableName tableName, final List<Scan> scans,
			final ResultsExtractor<T> action);

	<T> List<List<T>> findParallel(TableName tableName, final List<Scan> scans,
			final RowMapper<T> action);

	// Distributed scanners
	<T> List<T> find(TableName tableName, final Scan scan,
			final AbstractRowKeyDistributor rowKeyDistributor,
			final RowMapper<T> action);

	<T> List<T> find(TableName tableName, final Scan scan,
			final AbstractRowKeyDistributor rowKeyDistributor, int limit,
			final RowMapper<T> action);

	<T> List<T> find(TableName tableName, final Scan scan,
			final AbstractRowKeyDistributor rowKeyDistributor, int limit,
			final RowMapper<T> action, final LimitEventHandler limitEventHandler);

	<T> T find(TableName tableName, final Scan scan,
			final AbstractRowKeyDistributor rowKeyDistributor,
			final ResultsExtractor<T> action);


	Result increment(TableName tableName, final Increment increment);

	/**
	 *
	 * @param tableName
	 * @param incrementList
	 * @return
	 */
	List<Result> increment(TableName tableName,
			final List<Increment> incrementList);

	long incrementColumnValue(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier, final long amount);

	long incrementColumnValue(TableName tableName, final byte[] rowName,
			final byte[] familyName, final byte[] qualifier, final long amount,
			final boolean writeToWAL);

	/**
	 * 
	 * @param tableName
	 *            the target table
	 * @param action
	 *            callback object that specifies the action
	 * @param <T>
	 *            action type
	 * @return the result object of the callback action, or null
	 */
	<T> T execute(TableName tableName, TableCallback<T> action);

	/**
	 * 使用给定的  family 扫描目标表。内容由给定的  action 处理，通常返回  domain 或  domain 的集合。
	 * 
	 * @param tableName
	 *            target table
	 * @param family
	 *            column family
	 * @param action
	 *            action handling the scanner results
	 * @param <T>
	 *            action type
	 * @return the result object of the callback action, or null
	 */
	<T> T find(TableName tableName, String family,
			final ResultsExtractor<T> action);

	/**
	 * 使用给定的  family 扫描目标表。内容由给定的  action 处理，通常返回  domain 或  domain 的集合。
	 * 
	 * @param tableName
	 *            target table
	 * @param family
	 *            column family
	 * @param qualifier
	 *            column qualifier
	 * @param action
	 *            action handling the scanner results
	 * @param <T>
	 *            action type
	 * @return the result object of the callback action, or null
	 */
	<T> T find(TableName tableName, String family, String qualifier,
			final ResultsExtractor<T> action);

	/**
	 * 使用给定的{@link扫描}对象扫描目标表。适用于对扫描过程进行最大限度的控制。
	 * 内容由给定的 action 处理，通常返回 domain 或 domain 的集合。
	 * 
	 * @param tableName
	 *            target table
	 * @param scan
	 *            table scanner
	 * @param action
	 *            action handling the scanner results
	 * @param <T>
	 *            action type
	 * @return the result object of the callback action, or null
	 */
	<T> T find(TableName tableName, final Scan scan,
			final ResultsExtractor<T> action);

	/**
	 * 使用给定的 column family 扫描目标表。内容由给定的 action 逐行处理，并返回domian的集合。
	 * 
	 * @param tableName
	 *            target table
	 * @param family
	 *            column family
	 * @param action
	 *            row mapper handling the scanner results
	 * @param <T>
	 *            action type
	 * @return a list of objects mapping the scanned rows
	 */
	<T> List<T> find(TableName tableName, String family,
			final RowMapper<T> action);

	/**
	 * 使用给定的 column family 扫描目标表。内容由给定的 action 逐行处理，并返回domian的集合。
	 * 
	 * 
	 * @param tableName
	 *            target table
	 * @param family
	 *            column family
	 * @param qualifier
	 *            column qualifier
	 * @param action
	 *            row mapper handling the scanner results
	 * @param <T>
	 *            action type
	 * @return a list of objects mapping the scanned rows
	 */
	<T> List<T> find(TableName tableName, String family, String qualifier,
			final RowMapper<T> action);

	/**
	 * 使用给定的{@link扫描}对象扫描目标表。适用于对扫描过程进行最大限度的控制。
	 * 内容由给定的 action 逐行处理，并返回 domain 的集合。
	 * 
	 * @param tableName
	 *            target table
	 * @param scan
	 *            table scanner
	 * @param action
	 *            row mapper handling the scanner results
	 * @param <T>
	 *            action type
	 * @return a list of objects mapping the scanned rows
	 */
	<T> List<T> find(TableName tableName, final Scan scan,
			final RowMapper<T> action);

	/**
	 * 从给定表中获取单个行。内容由给定的 action 映射。
	 * 
	 * @param tableName
	 *            target table
	 * @param rowName
	 *            row name
	 * @param mapper
	 *            row mapper
	 * @param <T>
	 *            mapper type
	 * @return object mapping the target row
	 */
	<T> T get(TableName tableName, String rowName, final RowMapper<T> mapper);

	/**
	 * Gets an individual row from the given table. The content is mapped by the
	 * given action.
	 * 从给定表中获取单个行。内容由给定的 action 映射。
	 * 
	 * @param tableName
	 *            target table
	 * @param rowName
	 *            row name
	 * @param familyName
	 *            column family
	 * @param mapper
	 *            row mapper
	 * @param <T>
	 *            mapper type
	 * @return object mapping the target row
	 */
	<T> T get(TableName tableName, String rowName, String familyName,
			final RowMapper<T> mapper);

	/**
	 * 从给定表中获取单个行。内容由给定的 action 映射。
	 * 
	 * @param tableName
	 *            target table
	 * @param rowName
	 *            row name
	 * @param familyName
	 *            family
	 * @param qualifier
	 *            column qualifier
	 * @param mapper
	 *            row mapper
	 * @param <T>
	 *            mapper type
	 * @return object mapping the target row
	 */
	<T> T get(TableName tableName, final String rowName,
			final String familyName, final String qualifier,
			final RowMapper<T> mapper);
	
}
